#!/bin/sh
# set -x 
#1. Structure:
#-lib
#--webui.jar
#-config
#--application.yml
#-scripts
#--launchWebui.sh
#
basedir=`dirname $0`
cd $basedir/..
basedir=`pwd`

PROCESS_NAME="webui"
LOG_BASE="/opt/wdl/webui"

# Make the logs link on local if not existing
makeLogDirLink() {
  if [ ! -d "${LOG_BASE}/logs" ]; then
    /bin/mkdir -p "${LOG_BASE}/logs"
  fi
}

# Run WebUI process.
run() {

echo "${0}: ${PROCESS_NAME} is starting..."

exec java \
    -Xmx512m \
    -server \
    -DProcess=${PROCESS_NAME} \
    -Dloader.path=file:${basedir}/lib/,file:${basedir}/static/ \
    -Dlogging.config=file:${basedir}/config/logback.xml \
	-verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=2 -XX:GCLogFileSize=10M -Xloggc:/opt/sbc/CurrRel/logs/webui-gc.log \
    -Dspring.config.location=file:${SBC_OAM_COMMON}/,file:${basedir}/config/ -jar ${basedir}/lib/webui.jar  \
     >> ${basedir}/logs/webui.stdout 2>> ${basedir}/logs/webui.stderr &
}

#========================
#    Main program
#========================
case $1 in
  start)
    makeLogDirLink
    run;
    ;;
  *)
    echo "Usage: ${0} { start | stop | monitor }" >&2
    exit 1
    ;;
esac